package com.seya.calculite.app;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by Arumugam on 10/9/2014.
 */
public class DetailAdapterExpandable extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<ExpandListGroupByDate> groups;

    public DetailAdapterExpandable(Context context, ArrayList<ExpandListGroupByDate> groups) {
        this.context = context;
        this.groups = groups;
        }

    public void addItem(Details item, ExpandListGroupByDate group) {
        if (!groups.contains(group)) {
            groups.add(group);
        }
        int index = groups.indexOf(group);
        ArrayList<Details> details = groups.get(index).getDetails();
        details.add(item);
        groups.get(index).setDetails(details);
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        ArrayList<Details> details = groups.get(groupPosition).getDetails();
        return details.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        ArrayList<Details> details = groups.get(groupPosition).getDetails();
        return details.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ExpandListGroupByDate group = (ExpandListGroupByDate) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.activity_detail_group, null);
        }
        TextView groupdate = (TextView) convertView.findViewById(R.id.group_date);
        groupdate.setText(group.getcDate());
        // TODO Auto-generated method stub
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        Details d = (Details) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater li = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = li.inflate(R.layout.activity_detail_item, null);
        }
        TextView serial = (TextView)convertView.findViewById(R.id.serial);
        TextView vehicle = (TextView)convertView.findViewById(R.id.lstvehicle);
        TextView company = (TextView)convertView.findViewById(R.id.lstcompany);
        TextView tdate = (TextView)convertView.findViewById(R.id.lstdate);
        TextView start = (TextView)convertView.findViewById(R.id.lststart);
        TextView end = (TextView)convertView.findViewById(R.id.lstend);
        TextView km = (TextView)convertView.findViewById(R.id.lstkm);
        TextView tag = (TextView)convertView.findViewById(R.id.lsttag);
        TextView ttime = (TextView)convertView.findViewById(R.id.lsttime);

        serial.setText(Integer.toString(childPosition+1));
        vehicle.setText(d.getVehicle());
        company.setText(d.getCompany());
        tdate.setText(d.getDatecreated());
        start.setText(d.getStartlocation());
        end.setText(d.getEndlocation());
        km.setText(d.getKm());
        tag.setText(d.getTag());

        ttime.setText(d.getTime());

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
